<template>
  <div class="register-container">
    <el-card class="register-card" shadow="always">
      <template #header>
        <div class="register-header">
          <h1 class="register-title">注册新用户</h1>
          <p class="register-subtitle">创建一个新账号开始使用</p>
        </div>
      </template>

      <el-form
        ref="registerFormRef"
        :model="form"
        :rules="rules"
        label-position="top"
        size="large"
        @submit.prevent="handleRegister"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="form.username"
            placeholder="请输入用户名"
            :prefix-icon="User"
            :disabled="submitting"
            clearable
          />
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input
            v-model="form.password"
            type="password"
            placeholder="请输入密码"
            :prefix-icon="Lock"
            :disabled="submitting"
            show-password
            @keyup.enter="handleRegister"
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            size="large"
            :loading="submitting"
            @click="handleRegister"
            class="register-button"
          >
            {{ submitting ? '注册中...' : '注册' }}
          </el-button>
        </el-form-item>
      </el-form>

      <div class="quick-actions">
        <el-divider>已经有账号？</el-divider>
        <el-button
          type="default"
          plain
          size="large"
          :icon="ArrowLeft"
          @click="goToLogin"
          class="back-login-button"
        >
          返回登录
        </el-button>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock, ArrowLeft } from '@element-plus/icons-vue'
import type { FormInstance, FormRules } from 'element-plus'
import { userApi, type RegisterRequest, type RegisterResponse } from '../api/user'

const router = useRouter()
const registerFormRef = ref<FormInstance>()
const submitting = ref(false)

const form = reactive<RegisterRequest>({
  username: '',
  password: ''
})

const rules: FormRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 2, max: 20, message: '用户名长度在 2 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
  ]
}

const handleRegister = async () => {
  if (!registerFormRef.value) return
  try {
    await registerFormRef.value.validate()
    submitting.value = true
    const payload: RegisterRequest = {
      username: form.username.trim(),
      password: form.password.trim()
    }
    const res: RegisterResponse = await userApi.register(payload)

    // 放宽成功条件：只要 data === true 即视为成功，兼容 code 为 0 或 1
    if (res && (res as any).data === true) {
      ElMessage.success('注册成功，请登录！')
      router.push('/login')
    } else {
      ElMessage.error(((res as any)?.msg as string) || '注册失败，请重试')
    }
  } catch (e) {
    // 表单校验失败或请求异常
  } finally {
    submitting.value = false
  }
}

const goToLogin = () => router.push('/login')
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #fdf2f8 0%, #fce7f3 50%, #f9a8d4 100%);
  padding: 20px;
}

.register-card {
  width: 100%;
  max-width: 420px;
  border-radius: 16px;
  overflow: hidden;
}

.register-header {
  text-align: center;
  padding: 10px 0;
}

.register-title {
  font-size: 28px;
  font-weight: bold;
  color: #ec4899;
  margin: 0 0 8px 0;
}

.register-subtitle {
  font-size: 14px;
  color: #be185d;
  margin: 0;
  opacity: 0.8;
}

.register-button {
  width: 100%;
  height: 48px;
  font-size: 16px;
  font-weight: 600;
  background: linear-gradient(135deg, #ec4899, #db2777);
  border: none;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.register-button:hover {
  background: linear-gradient(135deg, #db2777, #be185d);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(236, 72, 153, 0.3);
}

.quick-actions {
  margin-top: 20px;
  text-align: center;
}

.back-login-button {
  width: 100%;
  height: 44px;
  font-size: 15px;
  border-radius: 8px;
}
</style>